<template>
  <div class="addEditComp">
    <el-dialog
      :before-close="handleBeforeClose"
      :title="titleStatus === 'add' ? '添加' : '编辑'"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleClose"
      @open="handleOpen"
    >
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="表情名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入表情名称"></el-input>
        </el-form-item>
        <el-form-item label="显示状态" prop="status">
          <el-radio v-model="form.status" :label="1">显示</el-radio>
          <el-radio v-model="form.status" :label="0">屏蔽</el-radio>
        </el-form-item>
        <el-form-item label="图标地址" prop="picture">
          <el-upload
            :http-request="customizeUpload"

            :on-success="handleSuccess"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <img v-if="form.picture" :src="form.picture" class="avatar"/>
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="起始特效" prop="special_start">
          <el-upload
            :http-request="customizeUpload"

            :on-success="handleSuccess1"
            :show-file-list="false"
            action=""

          >
            <div class="flex flex-col items-start">
              <el-button size="small" type="primary">点击上传</el-button>
              <p v-if="form.special_start" style="text-align:left">
                <i class="el-icon-tickets"></i> {{ form.special_start }}
              </p>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="运行特效">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess2"
            :show-file-list="false"
            action=""
            class="avatar-uploader"

          >
            <img
              v-if="form.special_running"
              :src="form.special_running"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-button
            v-if="form.special_running"
            type="danger"
            @click="handleRemove(3)"
          >删除
          </el-button>
        </el-form-item>
        <el-form-item label="结束特效" prop="special_end">
          <el-upload
            :http-request="customizeUpload"
            :on-success="handleSuccess3"
            :show-file-list="false"
            action=""
          >
            <div class="flex flex-col items-start">
              <el-button size="small" type="primary">点击上传</el-button>
              <p v-if="form.special_end" style="text-align:left">
                <i class="el-icon-tickets"></i> {{ form.special_end }}
              </p>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="表情价格" prop="name">
          <el-input
            v-model.number="form.price"
            placeholder="请输入表情价格"
          ></el-input>
        </el-form-item>
        <el-form-item label="最低爵位" prop="nobility_id">
          <el-select v-model="form.nobility_id">
            <el-option
              v-for="item in nobilityList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model.number="form.sort"
            placeholder="请输入排序"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
        >提交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>


import { postData, putEditData } from '@/api/interaction/expression'

export default {
  name: 'addEditComp',
  props: {
    titleStatus: {
      type: String,
      default: 'add'
    },
    dialogVisible: {
      type: Boolean,
      default: true
    },
    editForm: {
      type: Object
    },
    nobilityList: {
      type: Array
    }
  },
  data() {
    return {

      form: {
        id: undefined,
        name: undefined,
        status: 1,
        picture: undefined,
        special_start: undefined,
        special_running: undefined,
        special_end: undefined,
        start: undefined,
        running: undefined,
        end: undefined,
        price: undefined,
        sort: undefined,
        interval_time: undefined,
        nobility_id: undefined
      },
      rules: {
        name: [
          { required: 'true', message: '请输入表情名称', trigger: 'blur' }
        ],
        status: [
          { required: 'true', message: '请选择状态', trigger: 'change' }
        ],
        picture: [
          { required: 'true', message: '请上传图标', trigger: 'change' }
        ],
        special_start: [
          { required: 'true', message: '请上传起始特效', trigger: 'change' }
        ],
        special_running: [
          { required: 'true', message: '请上传运行特效', trigger: 'change' }
        ],
        special_end: [
          { required: 'true', message: '请上传结束特效', trigger: 'change' }
        ],

        price: [{ required: 'true', message: '请输入价格', trigger: 'blur' }],
        sort: [{ required: 'true', message: '请输入排序', trigger: 'blur' }],
        nobility_id: [
          { required: 'true', message: '请输入最低爵位ID', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleOpen() {
      if (this.editForm.id) {
        for (let key in this.editForm) {
          if (key === 'price') {
            this.form[key] = parseInt(this.editForm[key])
          } else {
            this.form[key] = this.editForm[key]
          }
        }
      }
    },
    handleBeforeClose() {
      this.$emit('update:dialogVisible', false)
    },
    handleClose() {
      this.form = {
        id: undefined,
        name: undefined,
        status: 1,
        picture: undefined,
        special_start: undefined,
        special_running: undefined,
        special_end: undefined,
        price: undefined,
        sort: undefined,
        nobility_id: undefined
      }
      this.$emit('update:dialogVisible', false)
    },
    // 图片上传
    handleSuccess() {
      this.form.picture = this.ossLastImgUrl
    },
    handleSuccess1() {
      this.form.special_start = this.ossLastImgUrl
    },
    handleSuccess2() {
      this.form.special_running = this.ossLastImgUrl
    },
    handleSuccess3() {
      this.form.special_end = this.ossLastImgUrl
    },
    handleRemove(type) {
      switch (type) {
        case 3:
          this.form.special_running = undefined
          break
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let data = {}
          for (let key in this.form) {
            // status 为必选项
            if (this.form[key] || key === 'status') {
              data[key] = this.form[key]
            }
          }

          const detail =
            this.titleStatus === 'add'
              ? await postData(data)
              : await putEditData(data)

          if (detail.code === 200) {
            this.$message.success(
              this.titleStatus === 'add' ? '添加成功!' : '编辑成功'
            )
            this.$emit('update:dialogVisible', false)
            this.$emit('updateTable')
          }
        } else {

          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border        : 1px dashed #d9d9d9;
  border-radius : 6px;
  cursor        : pointer;
  position      : relative;
  overflow      : hidden;
}

.avatar-uploader .el-upload:hover {
  border-color : #409eff;
}

.avatar-uploader-icon {
  font-size   : 28px;
  color       : #8c939d;
  width       : 100px;
  height      : 100px;
  line-height : 100px;
  text-align  : center;
}

.avatar {
  width   : 100px;
  height  : 100px;
  display : block;
}
</style>
